package com.um.jdy.common.commons.manager;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.um.jdy.common.platform.entity.po.App;
import com.um.jdy.common.platform.service.AppService;
import com.um.springboot.starter.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Component
public class WxManager {

    @Autowired
    WxMaService wxMaService;

    @Autowired
    WxPayService wxPayService;

    @Autowired
    WxMpService wxMpService;

    @Autowired
    AppService appService;

    /**
     * 通过comId获取对应微信小程序业务类
     * @param comId
     * @return
     */
    public WxMaService getMiniService(String comId) {
        App app = appService.getEntity("com_id", comId);
        if(null == app){
            throw new ServiceException("com_id不存在！");
        }

        WxMaDefaultConfigImpl maConfig = new WxMaDefaultConfigImpl();
        maConfig.setAppid(app.getMiniAppId());
        maConfig.setSecret(app.getMiniAppSecrect());

        try {
            wxMaService.addConfig(app.getComId(), maConfig);
            wxMaService.addConfig(app.getComId(), maConfig);
            return wxMaService.switchoverTo(comId);
        } catch (Exception e) {
            log.warn("微信小程序配置错误：{}", e.getMessage());
            throw new ServiceException("小程序配置错误！");
        }
    }

    /**
     * 通过comId获取对应微信支付业务类
     * @param comId
     * @return
     */
    public WxPayService getPayService(String comId) {
        App app = appService.getEntity("com_id", comId);
        if(null == app){
            throw new ServiceException("com_id不存在！");
        }

        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(app.getMiniAppId());
        payConfig.setMchId(app.getWxMchId());
        payConfig.setApiV3Key(app.getWxApiV3Key());
        payConfig.setPrivateKeyString(app.getWxKeyContent());
        payConfig.setPrivateCertString(app.getWxCertContent());

        try {
            wxPayService.addConfig(app.getComId(), payConfig);
            wxPayService.addConfig(app.getComId(), payConfig);
        } catch (Exception e) {
            log.warn("微信小程序配置错误：{}", e.getMessage());
        }

        return wxPayService.switchoverTo(comId);
    }

    /**
     * 通过comId获取对应微信支付业务类
     * @param comId
     * @return
     */
    public WxMpService getMpService(String comId) {
        App app = appService.getEntity("com_id", comId);
        if(null == app){
            throw new ServiceException("com_id不存在！");
        }

        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
        config.setAppId(app.getMpAppId());
        config.setSecret(app.getMpAppSecrect());
        try {
            wxMpService.addConfigStorage(app.getComId(), config);
            wxMpService.addConfigStorage(app.getComId(), config);
        } catch (Exception e) {
            log.warn("微信公众号配置错误：{}", e.getMessage());
        }

        return wxMpService.switchoverTo(comId);
    }

}
